package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.cardmanagement.util.CardLoss;
import com.turing.post.cardmanagement.util.CardLossDao;
import com.turing.post.cardmanagement.util.CardManaApplicationException;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "cardmanagement.impl.CardLossDaoImpl")
@Remote
@Local
public class CardLossDaoImpl implements CardLossDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public CardLoss getCardLossByCardLossId(BigInteger cardLossId) {
		return _em.find(CardLoss.class, cardLossId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public CardLoss getCardLossByCardLossIdForUpdate(BigInteger cardLossId) {
		Query query = _em
				.createNativeQuery(
						"select t.* from CardLoss t where t.cardLossId = ?1 for update",
						CardLoss.class);
		query.setParameter(1, cardLossId);
		List<CardLoss> cllist = query.getResultList();
		if (null == cllist || cllist.size() != 1) {
			return null;
		}
		CardLoss cl = cllist.get(0);
		_em.refresh(cl);
		return cl;
	}

	@Override
	public void insert(CardLoss cl) throws PCPSApplicationException {
		GeneralDao.insert(_em, cl);
	}

	@Override
	public void update(CardLoss cl) throws PCPSApplicationException {
		GeneralDao.update(_em, cl);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CardLoss> getCardLossListByCondition(PCPSWebInput webInput,
			String cardNo, String tranType, String cardHolder,
			String startTime, String endTime, String saleBranchNo, Integer page) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select t.* from cardloss t where 1=1 ";
		sql += PCPSUtil.getSqlCond(webInput, saleBranchNo, "branchNo");
		if (!PCPSUtil.isEmpty(cardNo)) {
			sql += " and t.cardno=" + cardNo;
		}
		if (!PCPSUtil.isEmpty(tranType)) {
			sql += " and t.tranType=" + tranType;
		}
		if (!PCPSUtil.isEmpty(cardHolder)) {
			sql += " and t.cardholder like '%" + cardHolder + "%'";
		}
		// 起始时间
		if (!PCPSUtil.isEmpty(startTime)) {
			sql = sql + " and t.trantime>=to_date('" + startTime
					+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		// 终止时间
		if (!PCPSUtil.isEmpty(endTime)) {
			sql = sql + " and t.trantime<= to_date('" + endTime
					+ " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		sql += " order by t.trantime desc";
		Query query = _em.createNativeQuery(sql, CardLoss.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		List<CardLoss> list = query.getResultList();
		return list;
	}

	@Override
	public BigInteger getCardLossTotalByCondition(PCPSWebInput webInput,
			String cardNo, String tranType, String cardHolder,
			String startTime, String endTime, String saleBranchNo) {
		String sql = "select nvl(count(*),0) from cardloss t where 1=1 ";
		sql += PCPSUtil.getSqlCond(webInput, saleBranchNo, "branchNo");
		if (!PCPSUtil.isEmpty(cardNo)) {
			sql += " and t.cardno=" + cardNo;
		}
		if (!PCPSUtil.isEmpty(tranType)) {
			sql += " and t.tranType=" + tranType;
		}
		if (!PCPSUtil.isEmpty(cardHolder)) {
			sql += " and t.cardholder like '%" + cardHolder + "%'";
		}
		// 起始时间
		if (!PCPSUtil.isEmpty(startTime)) {
			sql = sql + " and t.trantime>=to_date('" + startTime
					+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
		// 终止时间
		if (!PCPSUtil.isEmpty(endTime)) {
			sql = sql + " and t.trantime<= to_date('" + endTime
					+ " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
		}
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void updateCardLossImageId(BigInteger cardLossId, BigInteger imageId,
			boolean is) throws PCPSApplicationException {
		CardLoss cl = getCardLossByCardLossId(cardLossId);
		if (cl == null) {
			throw new CardManaApplicationException(
					CardManaApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "卡挂失信息不存在!");
		}
		if (is) {
			cl.setLossIdImageId(imageId);
		} else {
			cl.setProxyIdImageId(imageId);
		}
		update(cl);
	}
}
